<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>角色管理</title>
<!-- 
<link rel="stylesheet" type="text/css" href="../../lib/dijit/themes/soria/soria.css" />
<link rel="stylesheet" type="text/css" href="../../css/layout.css" />	
<script type="text/javascript" src="../../lib/dojo/dojo.js"></script>
 -->
<script>
//dojo.require("comba.component.Rest");
//var HMSRest=new comba.component.Rest();

function getHtml(data){
	var html = "";
	for(var i=0; i<data.length; i++){
		var major = data[i];
		//for(var maKey in major){
			html += '<table width="98%" border="0" cellspacing="0" cellpadding="2" class="usergroup">' + 
					'	<tr>' + 
					'		<th width="20" align="right"><input type="checkbox" id="ma_' + major.id + '" onclick="checkAll(this, \'major\')" /></th>' + 
					'		<th colspan="16" align="left">' + major.name + '</th>' + 
					'	</tr>'; 
			var minors = major["authClasses"];
			for(var j=0; j<minors.length; j++){
				var minor = minors[j];
				//for(var miKey in minor){
					var auths = minor["authInfos"];
					html +=	'	<tr>' + 
							'		<td align="right">&nbsp;</td>' + 
							'		<td align="right" valign="top" width="15"><input type="checkbox" id="mi_' + minor.id + '"  major="ma_' + major.id + '" onclick="checkAll(this, \'minor\')" /></td>' + 
							'		<td align="left" valign="top" width="150">' + minor.name + '</td>' + 
							'		<td colspan="14" align="left">' ; 
//							+ '		<table border="0" align="left" cellpadding="0" cellspacing="0">'+

//							'			<tr>';
						var k = 0;	
						for(; k<auths.length; k++){
							var auth = auths[k];
							//html += '				<td width="20px" align="right"><input type="checkbox" onclick="check(this);" name="auth" major="ma_' + major.id + '" minor="mi_' + minor.id + '" value="' + auth.id + '"/></td>' + 
							//		'				<td width="128px" align="left" >' + auth.name + '</td>';
							html +=  '<div><input type="checkbox" onclick="check(this);" name="auth" major="ma_' + major.id + '" minor="mi_' + minor.id + '" value="' + auth.id + '"/>' + auth.name + '</div>'
//							if(k%7 == 6){
//								html += "</tr><tr>"
//							}
							
						}
//						for(; k%7!=0; k++){
//							html +=	'				<td width="20px" align="right">&nbsp;</td><td width="128px">&nbsp;</td>';
//						}
						
					html +=	//'			</tr>' + 
							//'		</table>' + 
							'		</td>' + 
							'	</tr>';
				//	break;
				//}
			}
			html +=	'</table>';
		//			+ '<br />';
		//	break;
		//}
	}
	return html;
		
}

function check(node){
	if(!node.checked){
		document.getElementById(node.getAttribute("major")).checked = node.checked;
		document.getElementById(node.getAttribute("minor")).checked = node.checked;
	}
}
function checkAll(node, attr){
	if(!node.checked){
		var major = document.getElementById(node.getAttribute("major"));
		if(major){
			major.checked = node.checked;
		}
	}
	var elems = node.form.elements;
	for(var i=0;i<elems.length;i++){
		if(elems[i].tagName == "INPUT"){
			if(elems[i].getAttribute(attr)==node.id){
				elems[i].checked = node.checked;
			}
		}
	}
}
function copyBuildInRole(){
	_cleanAuthCheck();
	var selectCtrl = document.getElementById("buildInRoles");
	var opt = selectCtrl.options[selectCtrl.selectedIndex];
	var roleId = opt.value ;
	HMSRest.AjaxGet("../../service/role/fetch/"+roleId, function(response,ioArgs){
		var frm = document.getElementById("SecurityConfig");//self._roleConfigForm;
		var authsData = response.fakeAuths;
		for (var i=0;i<frm.length ;++i )
		{
			 var node = frm.elements[i];
			 var authId = node.value;
			 for (var j=0;j<authsData.length;++j )
			 {
				if (authId == authsData[j].id)
				{
					node.checked=true;
				}
			 }
		}
	}, function(response,ioArgs){alert(response)});
}
function _cleanAuthCheck(){
	var self = this;
	var frm = document.getElementById("SecurityConfig");//self._roleConfigForm;
	for (var i=0;i<frm.length ;++i )
	{
		if (frm.elements[i].name != "auth" && !/^m[ai]_/.test(frm.elements[i].id)){
			continue;
		}
		frm.elements[i].checked =false;
	}
}
function _fillRoleData(roleId){
	var frm = document.getElementById("SecurityConfig");//self._roleConfigForm;
	HMSRest.AjaxGet("../../service/role/fetch/"+roleId,
		function(response,ioArgs){
			frm.name.value=response.name;
			frm.memo.value=response.memo;
			 if (response.nonLocked=1)
			 {
				 frm.nonLocked.checked=true;
			 }else{
				  frm.nonLocked.checked=false;
			 }
			 var authsData = response.fakeAuths;
			 for (var i=0;i<frm.length ;++i )
			 {
				 var node = frm.elements[i];
				 var authId = node.value;
				 for (var j=0;j<authsData.length;++j )
				 {
					if (authId == authsData[j].id)
					{
						node.checked=true;
					}
				 }
			 }
		}
		,function(response,ioArgs){alert(response)});
}
var _role_self={_roleId: <%=request.getParameter("id")%>};

function _save(){
	var self = _role_self;
	var data ={};
	var frm = document.getElementById("SecurityConfig");//self._roleConfigForm;
	data["name"]=frm.name.value;
	data["memo"]=frm.memo.value;
	data["nonLocked"]=frm.nonLocked.checked?1:0;
	data["typeId"]=1;
	data["deleteFlag"]=false;
	var fakeAuths=[];
	

	var authsData="";
	for (var i=0;i<frm.length ;++i )
	{
		if (frm.elements[i].name != "auth"){
			continue;
		}
		if (frm.elements[i].checked == true){
			fakeAuths.push({"id":frm.elements[i].value})
		}
	}
	data["fakeAuths"]=fakeAuths;

	//var rest = new comba.component.Rest();
	
	if (self._roleId==null){
	HMSRest.AjaxPost("../../service/role", dojo.toJson(data),
		function(response,ioArgs){
		if (response.errorCode==0){
			frm.innerHTML = "<div style='height:80%;width:100%;text-align:center'><br/><br/><br/><br/><br/><br/>添加角色成功！</div>"
			//alert("添加角色成功！");
			//self._onClose();
			//self.parentDom.grid.rowSelectCell.toggleAllSelection(false);
			//CommonAction.refreshAction(self.parentDom);
		}else{
			
			alert(response.errorDesc);
			frm.name.value="";
			
		}},
		function(error,ioArgs){
			alert(error);
		}
	 );
	}else{
		HMSRest.AjaxPut("../../service/role/" + self._roleId,dojo.toJson(data),
				function(response,ioArgs){
				if (response.errorCode==0){
					frm.innerHTML = "<div style='height:80%;width:100%;text-align:center'><br/><br/><br/><br/><br/><br/>修改角色成功！</div>"
					//alert("修改角色成功！");
					//self._onClose();
					//self.parentDom.grid.rowSelectCell.toggleAllSelection(false);
					//CommonAction.refreshAction(self.parentDom);
				}else{
					alert(response.errorDesc);
					frm.name.value="";
				}},
				function(error,ioArgs){
					alert(error);
				}
			 );
	}

}
</script>
</head>
<body>
<form id="SecurityConfig">
<fieldset class="margin8"><legend><span>基本信息</span></legend>
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="1">
	<tr>
		<td width="7%" align="right">角色名称</td>
		<td width="16%" align="left"><input type="text" name="name" /></td>
		<td width="9%" align="right">是否开锁</td>
		<td width="4%" align="left"><input type="checkbox" name="nonLocked" /></td>
		<td width="7%" align="right">描述</td>
		<td width="17%" align="left"><input type="text" name="memo" /></td>
		<td width="11%" align="right">预定义角色</td>
		<td width="5%" align="left"><select name="buildInRoles" id="buildInRoles">
		</select></td>
		<td width="24%" align="left"><input type="button" value="复制角色授权" onclick="copyBuildInRole();" /></td>
	</tr>
</table>
</fieldset>
<fieldset class="margin8"><legend><span>授权信息</span></legend>
<div id="info"></div>
</fieldset>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td height="40" align="center"><input type="button" onclick="_save()" value="保存" /> <input
			type="reset" value="重置" /></td>
	</tr>
</table>
</form>
<script>
function _onload(){
	HMSRest.AjaxGet("../../service/auths2", function(response, ioArgs) {
		var html = getHtml(response.items);
		document.getElementById("info").innerHTML = html;
		if(_role_self._roleId){
			_fillRoleData(_role_self._roleId);
		}
	}, function(response, ioArgs) {
		alert(response)
	});

	HMSRest.AjaxGet("../../service/roles/buildinRoles", function (response,ioArgs){
				var data = response.items;
				var selectCtrl = document.getElementById("buildInRoles");
				while (selectCtrl.options.length>0){
					selectCtrl.remove(0);
				}
				for (var i=0;i<response.count;++i){
					item = data[i];
					opt = document.createElement('option');
					opt.value=item.id;
					opt.text=item.name;
					selectCtrl.add(opt,null);
				}
			}
			,function(response,errArgs)
			{}
		);
}
_onload();
</script>
</body>
</html>
